<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/4
 * Time: 14:32
 */
namespace App\Admin\Controllers;
use App\Http\Controllers\Controller;
use App\Models\AreaForm;
use App\Models\AreaFormReal;
use App\Models\PlayerOrder;
use App\Models\AreaFormPlayerGroup;
use App\Models\AreaFormProduct;

use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Grid;
use Illuminate\Http\Request;
use \Illuminate\Support\Facades\Input;
use App\Admin\Extra\Search\Html;
use Redirect;

class AreaFormController extends Controller{

    public function anyListAccount()
    {
        $req_param = Input::all();
        if ($req_param['cp_area'] == env('CP_AREA_PASSWD')) {
            return redirect(\App\Service\Common::build_url_query('admin/areaform/list-deal', $req_param) );
        }
        //OrderForm::getAccountByName();
        return Admin::content(function (Content $content)use($req_param) {

            $content->header('Area form');
            $content->description('服务器列表');

            $period_7 = \App\Service\Common::GetSomeDayPeriod(7, time());
            $param_7_day_arr_all = ['charge_ok' => 1, 'created_time' => ['start' => \App\Service\Common::GTF($period_7[0])]]; 
            $period_3 = \App\Service\Common::GetSomeDayPeriod(3, time());
            $param_3_day_arr_all = ['charge_ok' => 1, 'created_time' => ['start' => \App\Service\Common::GTF($period_3[0])]];
            $param_1_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", time() ) ]];
            $param_2_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", strtotime('-1 day') ), 'end'=> date("Y-m-d", strtotime('-0 day') ) ] ];
            $param_3_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", strtotime('-2 day') ), 'end'=> date("Y-m-d", strtotime('-1 day') ) ]];
            $param_4_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", strtotime('-3 day') ), 'end'=> date("Y-m-d", strtotime('-2 day') ) ]];
            $param_5_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", strtotime('-4 day') ), 'end'=> date("Y-m-d", strtotime('-3 day') ) ]];

            $content->row(Html::append([
                Html::input('cp_area', 'cp_area', url()->current()),
                Html::button('GO', url()->current(), 'search-filter'),

                Input::get('charge_ok', 1) ? Html::button('Charge OK', \App\Service\Common::build_url_query(url()->current(), ['charge_ok' => 0]), 'btn-goto btn-info')
                    : Html::button('Precharge', \App\Service\Common::build_url_query(url()->current(),  ['charge_ok' => 1]), 'btn-goto btn-default'),

                Html::button('Day 1', \App\Service\Common::build_url_query(url()->current(),  $param_1_day_arr), 'btn-goto btn-warning'),
                Html::button('Day 2', \App\Service\Common::build_url_query(url()->current(),  $param_2_day_arr), 'btn-goto btn-warning'),
                Html::button('Day 3', \App\Service\Common::build_url_query(url()->current(),  $param_3_day_arr), 'btn-goto btn-warning'),
                Html::button('Day 4', \App\Service\Common::build_url_query(url()->current(),  $param_4_day_arr), 'btn-goto btn-warning'),
                Html::button('Day 5', \App\Service\Common::build_url_query(url()->current(),  $param_5_day_arr), 'btn-goto btn-warning'),

                Html::button('1-3 Days ', \App\Service\Common::build_url_query(url()->current(),  $param_3_day_arr_all), 'btn-goto btn-primary'),
                Html::button('1-7 Days ', \App\Service\Common::build_url_query(url()->current(),  $param_7_day_arr_all), 'btn-goto btn-primary'),
                Html::sjs(\App\Service\Common::build_url_query(url()->current(),  ['cp_area' => $area]))
            ]));

            $grid = Admin::grid(AreaForm::class, function(Grid $grid) use($req_param) {

//                $grid->model()->where('deleted_at', 0);
                $grid->actions(function($actions){
                    $actions->disableDelete();
                    $actions->disableEdit();
                    $actions->append('<a href="'.url('admin/areaorder/list-all?cp_area='). $actions->row->cp_area .'"><i class="glyphicon glyphicon-eye-open"></i></a>');
                });
                $area_data = \App\Models\Area::areaSelectData(0);

                // 第一列显示id字段，并将这一列设置为可排序列
                $grid->cp_area('cp_area')->display(function($value)use($area_data) {
                    return $area_data[$value] . "(${value})";
                });;
                $grid->model()->orderBy('cp_area', 'DESC');

                // 第二列显示title字段，由于title字段名和Grid对象的title方法冲突，所以用Grid的column()方法代替
                $grid->sum_dollar('sumDollar')->display(function() {
                    return '$ ' . $this->sum_dollar;
                });
                $grid->sum_rmb('sumRmb')->display(function() {
                    return '￥ ' . $this->sum_rmb;
                });
                $grid->ChargeNo('Charge Num')->display(function($value) {
                    $req_param['cp_area'] = $this->cp_area;
                    return Html::button($value, \App\Service\Common::build_url_query(url('admin/areaorder/list-all'),  $req_param), 'btn-goto btn-inverse');
                });
                $grid->ticket('红钻活动')->display(function($value) {
                    $req_param['cp_area'] = $this->cp_area;
                    $req_param['op_time'] = $req_param['created_time'];
                    return Html::button('ActivityTicket', \App\Service\Common::build_url_query(url('admin/areaorder/ticket-total'),  $req_param), 'btn-goto btn-inverse');
                });
                $grid->LTV('LTV')->display(function($value) {
                    $req_param['cp_area'] = $this->cp_area;
                    return Html::button('LTV', \App\Service\Common::build_url_query(url('admin/payst/recharge-role'),  $req_param), 'btn-goto btn-inverse');
                });

                $grid->filter(function ($filter){

                    $filter->like('cp_player_id', 'cp_player_id')->placeholder('');
                    $filter->equal('OrderStatus', 'OrderStatus')->select(\App\Models\AreaOrder::$order_status);
                    $filter->between('created_time', 'Created Time')->datetime();

                });
            });
            $content->row($grid->render());
        });


    }

    // 隐秘显示给我们自己的地址
    public function anyListDeal()
    {
        $req_param = Input::all();
        //OrderForm::getAccountByName();
        return Admin::content(function (Content $content)use($req_param) {

            $content->header('Area form');
            $content->description('服务器列表');

            $period_7 = \App\Service\Common::GetSomeDayPeriod(7, time());
            $param_7_day_arr_all = ['charge_ok' => 1, 'created_time' => ['start' => \App\Service\Common::GTF($period_7[0])]]; 
            $period_3 = \App\Service\Common::GetSomeDayPeriod(3, time());
            $param_3_day_arr_all = ['charge_ok' => 1, 'created_time' => ['start' => \App\Service\Common::GTF($period_3[0])]];
            $param_1_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", time() ) ]];
            $param_2_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", strtotime('-1 day') ), 'end'=> date("Y-m-d", strtotime('-0 day') ) ] ];
            $param_3_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", strtotime('-2 day') ), 'end'=> date("Y-m-d", strtotime('-1 day') ) ]];
            $param_4_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", strtotime('-3 day') ), 'end'=> date("Y-m-d", strtotime('-2 day') ) ]];
            $param_5_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", strtotime('-4 day') ), 'end'=> date("Y-m-d", strtotime('-3 day') ) ]];

            $content->row(Html::append([
                Html::input('cp_area', 'cp_area', url()->current()),
                Html::button('GO', url()->current(), 'search-filter'),

                Input::get('charge_ok', 1) ? Html::button('Charge OK', \App\Service\Common::build_url_query(url()->current(), ['charge_ok' => 0]), 'btn-goto btn-info')
                    : Html::button('Precharge', \App\Service\Common::build_url_query(url()->current(),  ['charge_ok' => 1]), 'btn-goto btn-default'),

                Html::button('Day 1', \App\Service\Common::build_url_query(url()->current(),  $param_1_day_arr), 'btn-goto btn-warning'),
                Html::button('Day 2', \App\Service\Common::build_url_query(url()->current(),  $param_2_day_arr), 'btn-goto btn-warning'),
                Html::button('Day 3', \App\Service\Common::build_url_query(url()->current(),  $param_3_day_arr), 'btn-goto btn-warning'),
                Html::button('Day 4', \App\Service\Common::build_url_query(url()->current(),  $param_4_day_arr), 'btn-goto btn-warning'),
                Html::button('Day 5', \App\Service\Common::build_url_query(url()->current(),  $param_5_day_arr), 'btn-goto btn-warning'),

                Html::button('1-3 Days ', \App\Service\Common::build_url_query(url()->current(),  $param_3_day_arr_all), 'btn-goto btn-primary'),
                Html::button('1-7 Days ', \App\Service\Common::build_url_query(url()->current(),  $param_7_day_arr_all), 'btn-goto btn-primary'),
                Html::sjs(\App\Service\Common::build_url_query(url()->current(),  ['cp_area' => $area]))
            ]));

            $grid = Admin::grid(AreaFormReal::class, function(Grid $grid) use($req_param) {

//                $grid->model()->where('deleted_at', 0);
                $grid->actions(function($actions){
                    $actions->disableDelete();
                    $actions->disableEdit();
                    $actions->append('<a href="'.url('admin/areaorder/list-all?cp_area='). $actions->row->cp_area .'"><i class="glyphicon glyphicon-eye-open"></i></a>');
                });
                $area_data = \App\Models\Area::areaSelectData(0);

                // 第一列显示id字段，并将这一列设置为可排序列
                $grid->cp_area('cp_area')->display(function($value)use($area_data) {
                    return $area_data[$value] . "(${value})";
                });;
                $grid->model()->orderBy('cp_area', 'DESC');

                // 第二列显示title字段，由于title字段名和Grid对象的title方法冲突，所以用Grid的column()方法代替
                $grid->sum_dollar('sumDollar')->display(function() {
                    return '$ ' . $this->sum_dollar;
                });
                $grid->sum_rmb('sumRmb')->display(function() {
                    return '￥ ' . $this->sum_rmb;
                });
                $grid->sum_deal('sumDeal')->display(function() {
                    return '$ ' . $this->sum_deal;
                });
                $grid->ChargeNo('Charge Num')->display(function($value) {
                    $req_param['cp_area'] = $this->cp_area;
                    return Html::button($value, \App\Service\Common::build_url_query(url('admin/areaorder/list-all'),  $req_param), 'btn-goto btn-inverse');
                });
                $grid->LTV('LTV')->display(function($value) {
                    $req_param['cp_area'] = $this->cp_area;
                    return Html::button('LTV', \App\Service\Common::build_url_query(url('admin/payst/recharge-role'),  $req_param), 'btn-goto btn-inverse');
                });
                $grid->filter(function ($filter) use ($area_data){

                    $filter->like('cp_player_id', 'cp_player_id')->placeholder('');
                    $filter->equal('cp_area', 'Area')->select($area_data);
                    $filter->equal('OrderStatus', 'OrderStatus')->select(\App\Models\AreaOrder::$order_status);
                    $filter->between('created_time', 'Created Time')->datetime();

                });
            });
            $content->row($grid->render());
        });


    }
    // 玩家充值 金额 汇总值
    public function anyAreaOrderPlayerGroup() {
        $req_param = Input::all();

        //OrderForm::getAccountByName();
        return Admin::content(function (Content $content)use($req_param) {

            $content->header('Area form');
            $content->description('服务器列表');

            $period_7 = \App\Service\Common::GetSomeDayPeriod(7, time());
            $param_7_day_arr_all = ['charge_ok' => 1, 'created_time' => ['start' => \App\Service\Common::GTF($period_7[0])]]; 
            $period_3 = \App\Service\Common::GetSomeDayPeriod(3, time());
            $param_3_day_arr_all = ['charge_ok' => 1, 'created_time' => ['start' => \App\Service\Common::GTF($period_3[0])]];
            $param_1_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", time() ) ]];
            $param_2_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", strtotime('-1 day') ), 'end'=> date("Y-m-d", strtotime('-0 day') ) ] ];
            $param_3_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", strtotime('-2 day') ), 'end'=> date("Y-m-d", strtotime('-1 day') ) ]];
            $param_4_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", strtotime('-3 day') ), 'end'=> date("Y-m-d", strtotime('-2 day') ) ]];
            $param_5_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", strtotime('-4 day') ), 'end'=> date("Y-m-d", strtotime('-3 day') ) ]];

            $content->row(Html::append([
                Html::input('cp_area', 'cp_area', url()->current()),
                Html::button('GO', url()->current(), 'search-filter'),

                Input::get('charge_ok', 1) ? Html::button('Charge OK', \App\Service\Common::build_url_query(url()->current(), ['charge_ok' => 0]), 'btn-goto btn-info')
                    : Html::button('Precharge', \App\Service\Common::build_url_query(url()->current(),  ['charge_ok' => 1]), 'btn-goto btn-default'),

                Html::button('Today', \App\Service\Common::build_url_query(url()->current(),  $param_1_day_arr), 'btn-goto btn-warning'),
                Html::button('Day 2', \App\Service\Common::build_url_query(url()->current(),  $param_2_day_arr), 'btn-goto btn-warning'),
                Html::button('Day 3', \App\Service\Common::build_url_query(url()->current(),  $param_3_day_arr), 'btn-goto btn-warning'),
                Html::button('Day 4', \App\Service\Common::build_url_query(url()->current(),  $param_4_day_arr), 'btn-goto btn-warning'),
                Html::button('Day 5', \App\Service\Common::build_url_query(url()->current(),  $param_5_day_arr), 'btn-goto btn-warning'),

                Html::button('1-3 Days ', \App\Service\Common::build_url_query(url()->current(),  $param_3_day_arr_all), 'btn-goto btn-primary'),
                Html::button('1-7 Days ', \App\Service\Common::build_url_query(url()->current(),  $param_7_day_arr_all), 'btn-goto btn-primary'),
                Html::sjs(\App\Service\Common::build_url_query(url()->current(),  ['cp_area' => $area]))
            ]));

            $grid = Admin::grid(AreaFormPlayerGroup::class, function(Grid $grid) use($req_param) {

    //                $grid->model()->where('deleted_at', 0);
                $grid->actions(function($actions){
                    $actions->disableDelete();
                    $actions->disableEdit();
                    $actions->append('<a href="'.url('admin/areaorder/list-all?cp_area='). $actions->row->cp_area .'"><i class="glyphicon glyphicon-eye-open"></i></a>');
                });
                $area_data = \App\Models\Area::areaSelectData(0);
                $grid->cp_player_id('Player Id')->display(function($value)use($area_data) {
                    return $value;
                });
                $grid->cp_account('Account')->display(function($value)use($area_data) {
                    return '<a href = "'.url('admin/player/show?accountId='). $value .'" >'.$value. '</a>';
                });
                // 第一列显示id字段，并将这一列设置为可排序列
                $grid->cp_area('cp_area')->display(function($value)use($area_data) {
                    return $area_data[$value] . "(${value})";
                });
                $grid->model()->orderBy('cp_area', 'DESC');

                // 第二列显示title字段，由于title字段名和Grid对象的title方法冲突，所以用Grid的column()方法代替
                $grid->sum_dollar('sumDollar')->display(function() {
                    return '$ ' . $this->sum_dollar;
                });
                $grid->sum_rmb('sumRmb')->display(function() {
                    return '￥ ' . $this->sum_rmb;
                })->sortable();

                if ($req_param['cp_area'] == env('CP_AREA_PASSWD')) {
                    $grid->sum_deal('sumDeal')->display(function() {
                        return '$ ' . $this->sum_deal;
                    });
                }

                $grid->ChargeNo('Charge Num')->display(function($value) {
                    $req_param['cp_area'] = $this->cp_area;
                    $req_param['cp_player_id'] = $this->cp_player_id;
                    return Html::button($value, \App\Service\Common::build_url_query(url('admin/areaorder/list-all'),  $req_param), 'btn-goto btn-inverse');
                });
                $grid->createTime('AccountCreateTime')->sortable();

                $grid->filter(function ($filter) use($area_data){

                    $filter->like('cp_player_id', 'cp_player_id')->placeholder('');
                    $filter->equal('cp_area', 'Area')->select($area_data);
                    $filter->equal('OrderStatus', 'OrderStatus')->select(\App\Models\AreaOrder::$order_status);
                    $filter->between('created_time', 'Created Time')->datetime();

                });
            });
            $content->row($grid->render());
        });       
    }

    // 玩家充值 金额 汇总值
    public function anyAreaFormProduct() {
        $req_param = Input::all();

        //OrderForm::getAccountByName();
        return Admin::content(function (Content $content)use($req_param) {

            $content->header('Area form');
            $content->description('服务器列表');

            $period_7 = \App\Service\Common::GetSomeDayPeriod(7, time());
            $param_7_day_arr_all = ['charge_ok' => 1, 'created_time' => ['start' => \App\Service\Common::GTF($period_7[0])]]; 
            $period_3 = \App\Service\Common::GetSomeDayPeriod(3, time());
            $param_3_day_arr_all = ['charge_ok' => 1, 'created_time' => ['start' => \App\Service\Common::GTF($period_3[0])]];
            $param_1_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", time() ) ]];
            $param_2_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", strtotime('-1 day') ), 'end'=> date("Y-m-d", strtotime('-0 day') ) ] ];
            $param_3_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", strtotime('-2 day') ), 'end'=> date("Y-m-d", strtotime('-1 day') ) ]];
            $param_4_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", strtotime('-3 day') ), 'end'=> date("Y-m-d", strtotime('-2 day') ) ]];
            $param_5_day_arr = ['charge_ok' => 1, 'created_time' => ['start' => date("Y-m-d", strtotime('-4 day') ), 'end'=> date("Y-m-d", strtotime('-3 day') ) ]];

            $content->row(Html::append([
                Html::input('cp_area', 'cp_area', url()->current()),
                Html::button('GO', url()->current(), 'search-filter'),

                Input::get('charge_ok', 1) ? Html::button('Charge OK', \App\Service\Common::build_url_query(url()->current(), ['charge_ok' => 0]), 'btn-goto btn-info')
                    : Html::button('Precharge', \App\Service\Common::build_url_query(url()->current(),  ['charge_ok' => 1]), 'btn-goto btn-default'),

                Html::button('Today', \App\Service\Common::build_url_query(url()->current(),  $param_1_day_arr), 'btn-goto btn-warning'),
                Html::button('Day 2', \App\Service\Common::build_url_query(url()->current(),  $param_2_day_arr), 'btn-goto btn-warning'),
                Html::button('Day 3', \App\Service\Common::build_url_query(url()->current(),  $param_3_day_arr), 'btn-goto btn-warning'),
                Html::button('Day 4', \App\Service\Common::build_url_query(url()->current(),  $param_4_day_arr), 'btn-goto btn-warning'),
                Html::button('Day 5', \App\Service\Common::build_url_query(url()->current(),  $param_5_day_arr), 'btn-goto btn-warning'),

                Html::button('1-3 Days ', \App\Service\Common::build_url_query(url()->current(),  $param_3_day_arr_all), 'btn-goto btn-primary'),
                Html::button('1-7 Days ', \App\Service\Common::build_url_query(url()->current(),  $param_7_day_arr_all), 'btn-goto btn-primary'),
                Html::sjs(\App\Service\Common::build_url_query(url()->current(),  ['cp_area' => $area]))
            ]));

            $grid = Admin::grid(AreaFormProduct::class, function(Grid $grid) use($req_param) {

    //                $grid->model()->where('deleted_at', 0);
                $grid->actions(function($actions){
                    $actions->disableDelete();
                    $actions->disableEdit();
                    $actions->append('<a href="'.url('admin/areaorder/list-all?cp_area='). $actions->row->cp_area .'"><i class="glyphicon glyphicon-eye-open"></i></a>');
                });
                $area_data = \App\Models\Area::areaSelectData(0);
                $grid->cp_product_id('Product Id')->display(function($value)use($area_data) {
                    return $value;
                })->sortable();
                $grid->prop_name('Product Name')->display(function($value)use($area_data) {
                    return $value;
                });
               
                $grid->model()->orderBy('cp_area', 'DESC');

                // 第二列显示title字段，由于title字段名和Grid对象的title方法冲突，所以用Grid的column()方法代替
                $grid->sum_dollar('sumDollar')->display(function() {
                    return '$ ' . $this->sum_dollar;
                })->sortable();
                $grid->sum_rmb('sumRmb')->display(function() {
                    return '￥ ' . $this->sum_rmb;
                })->sortable();

                if ($req_param['cp_area'] == env('CP_AREA_PASSWD')) {
                    $grid->sum_deal('sumDeal')->display(function() {
                        return '$ ' . $this->sum_deal;
                    })->sortable();
                }

                $grid->ChargeNo('Charge Num')->display(function($value) {
                    $req_param['cp_area'] = $this->cp_area;
                    $req_param['cp_product_id'] = $this->cp_product_id;
                    return Html::button($value, \App\Service\Common::build_url_query(url('admin/areaorder/list-all'),  $req_param), 'btn-goto btn-inverse');
                })->sortable();

                $grid->filter(function ($filter) use($area_data){

                    $filter->like('cp_product_id', 'cp_product_id')->placeholder('');
                    $filter->equal('cp_area', 'Area')->select($area_data);
                    $filter->equal('OrderStatus', 'OrderStatus')->select(\App\Models\AreaOrder::$order_status);
                    $filter->between('created_time', 'Created Time')->datetime();

                });
            });
            $content->row($grid->render());
        });       
    }
}